package client

import (
	. "chess_platform/internal/common"
	"fmt"
	"time"
)

//一些杂七杂八的

// 新建错误日志记录
func NewError(data map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChessLog)
	values, sql := InsertSql(TableClientError, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

// 新建错误日志记录
func NewSubmitLog(data map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChessLog2)
	values, sql := InsertSql(TableSubmitLog, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//更新打点日志为无效的日志
func UpdateSubmitLogInvalid(uid, interval int64) (int64, error) {
	o := NewDBOrm(DBChessLog2)
	end := time.Now().Unix()
	condition := fmt.Sprintf(`error_time >= %v AND error_time < %v AND uid = %v`, end-interval, end, uid)
	data := map[string]interface{}{
		"is_valid": 2,
	}
	values, sql := UpdateSql(TableSubmitLog, data, condition)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.RowsAffected()
}

// 新建错误日志记录(用户手动提交的)
func NewErrorLog(data map[string]interface{}) (int64, error) {
	o := NewDBOrm(DBChessLog)
	values, sql := InsertSql(TableClientOperationError, data)
	result, err := o.Raw(sql, values).Exec()
	if err != nil {
		return 0, err
	}
	return result.LastInsertId()
}

//获取一个可以删除的日志文件
func GetDeleteErrorLog(day int64) (string, error) {
	var url string
	sql := fmt.Sprintf(`
		SELECT
			log_url
		FROM
			%v
		WHERE
			error_time <= %v
		ORDER BY
			error_time
		LIMIT 1
		`, TableClientOperationError, day)
	o := NewDBOrm(DBChessLog)
	err := o.Raw(sql).QueryRow(&url)
	if CheckNoExist(err) {
		return "", nil
	}
	return url, err
}

//已做缓存, 其它配置列表(开屏广告,客服,合作)
func ListOtherConf(typ int64) ([]OtherConf, error) {
	where := "1=1"
	if typ != 0 {
		if typ >= 4 {
			where = fmt.Sprintf("type >= %v", typ)
		} else {
			where = fmt.Sprintf("type = %v", typ)
		}
	}
	var gl []OtherConf
	Key := fmt.Sprintf("%v%v", KeyOtherConfig, typ)
	err := GetCache(RedisCache, Key, &gl)
	if err != nil {
		sql := fmt.Sprintf(`
		SELECT
			*
		FROM
			%v
		WHERE
			%v
		`, TableOtherConf, where)
		o := NewDBOrm(DBChessSysData)
		_, err := o.Raw(sql).QueryRows(&gl)
		status := CheckErrorStatus(err)
		if status == NoExist || status == InvalidConn {
			return gl, nil
		} else if status == ErrorHappen {
			return gl, err
		}
		//只有err == nil才存在缓存里
		SetCache(RedisCache, Key, gl, CacheExpire)
	}
	return gl, nil
}

//跑马灯列表
func ListHorse(typ int64) ([]*Horse, error) {
	var gl []*Horse
	key := fmt.Sprintf("%v%v", KeyHorse, typ)
	err := GetCache(RedisCache, key, &gl)
	if err != nil {
		now := time.Now().Unix()
		where := fmt.Sprintf(` publish_time < %v AND expire_time > %v AND type = %v`, now, now, typ)
		sql := fmt.Sprintf(`
		SELECT
			content
		FROM
			%v
		WHERE
			%v
		ORDER BY
			publish_time DESC
		`, TableHorse, where)
		o := NewDBOrm(DBChessSysData)
		_, err := o.Raw(sql).QueryRows(&gl)
		status := CheckErrorStatus(err)
		if status == NoExist || status == InvalidConn {
			return gl, nil
		} else if status == ErrorHappen {
			return gl, err
		}
		//只有没有错的情况才存入缓存
		SetCache(RedisCache, key, gl, CacheExpire)
	}
	return gl, nil
}
